﻿using System.Web.Mvc;
using Century.Web.Security.Attributes;
using Telerik.Web.Mvc;
using Century.Web.UI.MVC;
using Century.Model;
using Century.EnterpriseLibrary.Globalization;
using WebMatrix.WebData;
using System.Linq;
using Century.Business.CMS;

namespace Century.Admin.Controllers
{

    [Authorization(Group = "", Name = "TranslationTextManager")]
    public class TranslationTextManagerController : CenturyAdminBaseController
    {
        public TranslationTextManagerController(ISiteService siteService) : base(siteService) { }

        //
        // GET: /TranslationTextManager/

        //TranslationTextBSO db = new TranslationTextBSO();
        public ActionResult Index()
        {
            return View();
        }

        [GridAction]
        [HttpPost]
        public ActionResult GetTextList(string templateName, string applicationName)
        {
            TempData["templateName"] = templateName;
            TempData["applicationName"] = applicationName;
            return View(TextList(templateName, applicationName));
        }

        [AcceptVerbs(HttpVerbs.Post)]
        [GridAction]
        [ValidateInput(false)]
        [HttpPost]
        public ActionResult UpdateText(int id)
        {
            TranslationText updateText = new TranslationText();
            TryUpdateModel(updateText);
            GlobalizationManager.UpdateLabelValue(updateText.TranslationKey, updateText.TranslationValue, Token.CurrentSite.Id);

            Log.InfoFormat("Label [{0}] edited by User [{1}] in site [{2}]", updateText.TranslationKey, WebSecurity.CurrentUserName, Token.CurrentSite.Path);

            //after updating grid is filled by updatetext method, so ı need to use getlistmethods parameters
            string templateName = TempData["templateName"] as string;
            string applicationName = TempData["applicationName"] as string;

            return View(TextList(templateName, applicationName));
        }

        public GridModel TextList(string templateName, string applicationName)
        {
            #region Getting label list

            if (string.IsNullOrEmpty(templateName) && (applicationName == "PleaseSelect" || applicationName == null))
            {
                var model = new GridModel
                {
                    Data = GlobalizationManager.GetAll(Token.CurrentSite.Id).OrderBy(c => c.TranslationKey).Select(c => new { c.Id, c.SiteId, c.TranslationKey, c.TranslationValue }).ToList()
                };
                return (model);
            }
            else if (string.IsNullOrEmpty(templateName.Trim()) && !string.IsNullOrEmpty(applicationName))
            {
                var model = new GridModel
                {
                    Data = GlobalizationManager.GetAll(Token.CurrentSite.Id).Where(c => c.TranslationKey.StartsWith(applicationName)).OrderBy(c => c.TranslationKey).ToList()
                    .Select(c => new
                    {
                        c.Id,
                        c.SiteId,
                        c.TranslationKey,
                        c.TranslationValue
                    })
                };
                return (model);
            }
            else if (!string.IsNullOrEmpty(templateName.Trim()) && applicationName == "PleaseSelect")
            {
                var model = new GridModel
                {
                    Data = GlobalizationManager.GetAll(Token.CurrentSite.Id).Where(c => c.TranslationValue.Contains(templateName.Trim())).OrderBy(c => c.TranslationKey).ToList()
                    .Select(c => new
                    {
                        c.Id,
                        c.SiteId,
                        c.TranslationKey,
                        c.TranslationValue
                    })
                };
                return (model);
            }
            else
            {
                var model = new GridModel
                {
                    Data = GlobalizationManager.GetAll(Token.CurrentSite.Id).Where(c => c.TranslationValue.Contains(templateName.Trim()) && c.TranslationKey.StartsWith(applicationName)).OrderBy(c => c.TranslationKey).ToList()
                   .Select(c => new
                   {
                       c.Id,
                       c.SiteId,
                       c.TranslationKey,
                       c.TranslationValue
                   })
                };
                return (model);
            }

            #endregion
        }
    }
}
